using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace View.Instant
{
    public class SettingView : View
    {
        private Button mbtnClose;
        private Toggle mtogMusic;
        private GameObject mobjMusicOpen;
        private GameObject mobjMusicClose;
        private Toggle mtogSound;
        private GameObject mobjSoundOpen;
        private GameObject mobjSoundClose;
        public override void OnLoad()
        {
           base.OnLoad();

           mbtnClose = transform.Find("btnclose").GetComponent<Button>();
           mtogMusic = transform.Find("yinxiao/music").GetComponent<Toggle>();
           mobjMusicOpen = transform.Find("yinxiao/music/open").gameObject;
           mobjMusicClose = transform.Find("yinxiao/music/close").gameObject;
           mtogSound = transform.Find("yinxiao/sound").GetComponent<Toggle>();
           mobjSoundOpen = transform.Find("yinxiao/sound/open").gameObject;
           mobjSoundClose = transform.Find("yinxiao/sound/close").gameObject;
            
           AddClickListener(mbtnClose, () =>
           {
               UIManager.Instance.Close(ViewName.SettingView);
           });
           mtogMusic.onValueChanged.AddListener((value_) =>
           {
               AudioSystem.Instance.OnPlayBtn();
               if (value_)
               {
                   AudioSystem.Instance.ResumeBgm();
                   GameLocalInfo.OnSetIsPlayBGM(true);
               }
               else
               {
                   AudioSystem.Instance.PauseBgm();
                   GameLocalInfo.OnSetIsPlayBGM(false);
               }
               mobjMusicOpen.SetActive(value_);
               mobjMusicClose.SetActive(!value_);
           });
           
           mtogSound.onValueChanged.AddListener((value_) =>
           {
               AudioSystem.Instance.OnPlayBtn();
               if (value_)
               {
                   AudioSystem.Instance.SetEffectAudioVolume(1);
                   GameLocalInfo.OnSetIsPlayEffect(true);
               }
               else
               {
                   AudioSystem.Instance.SetEffectAudioVolume(0);
                   GameLocalInfo.OnSetIsPlayEffect(false);
               }
               mobjSoundOpen.SetActive(value_);
               mobjSoundClose.SetActive(!value_);
           });
        }

        public override void OnShow(params object[] args)
        {
            base.OnShow();

            mtogMusic.isOn = GameLocalInfo.OnIsPlayBGM();
            mobjMusicOpen.SetActive(mtogMusic.isOn);
            mobjMusicClose.SetActive(!mtogMusic.isOn);
            
            mtogSound.isOn = GameLocalInfo.OnIsPlayEffect();
            mobjSoundOpen.SetActive(mtogSound.isOn);
            mobjSoundClose.SetActive(!mtogSound.isOn);
        }

        public override void OnClose(params object[] args)
        {
            base.OnClose();
        }
    }
} 

